<?php
namespace Admin\Controller;
use Think\Controller;
/*
后台问题管理  
 */
class QuestionController extends CommonController {
    //问题列表-------------------------------------------------------------
    public function question_list(){

      $data=I('get.');
      $stime=strtotime($data['starttime']);
      $etime=strtotime($data['endtime']);
      $title=$data['title'];
      //搜索某个人
      $sreach=M('question')->where("title='$title' and create_time>='$stime' and create_time<='$etime' ")->select();
      if ($sreach) {
          $this->assign('data',$sreach);
      }else{
          $count      = M('question')->where("admin_status='0'")->count();// 查询满足要求的总记录数
          $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出

          // $sql="select * from question limit ".$Page->firstRow.','.$Page->listRows;
          // // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
          // $data=M('question')->query($sql);

          $this->assign('page',$show);// 赋值分页输出

          $sql = "select t1.*,t2.name,t3.nickname from question t1 inner join category t2 on t1.cid=t2.id inner join user t3 on t1.uid=t3.id  where t1.admin_status='0' limit ".$Page->firstRow.','.$Page->listRows;// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

          $res = M()->query($sql);
          // var_dump($data);
          $this->assign('data',$res);
      }


        $this->display();
    }
    //问题列表 批量删除 封号还存在
    public function delall()
    {
      $uid = I('post.uid');
      $uid=rtrim($uid,',');
      // $id = explode(',', $uid);
      // var_dump($id);
      // $sql = "delete from question where id in (".$uid.")";
      $sql = "update question set admin_status='1' where id in (".$uid.")";//封号还存在
      $res = M('question')->execute($sql);

      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"没有删除");
      }

    }
    //问题列表  编辑模板
    public function question_edit()
    {
      // var_dump(8880);
      $id=I('get.id');

      //取子父类
      $cate = M('category')->select();
      $tempArr=[];
      foreach ($cate as $row) {
        if ($row['fid']==0) {
          $tempArr[$row['id']]=$row;
        }
      }
      foreach ($cate as $son) {
        foreach ($tempArr as $key => $fater) {
          if ($son['fid']==$key) {
            $tempArr[$key]['son'][]=$son;
          }
        }
      }
      $this->assign('cate',$tempArr);
      // var_dump($tempArr);
      $data=M('question')->where("id='$id'")->find();
      $this->assign('data',$data);
      // var_dump($data);
      $this->display();
    }
    //问题列表 编辑
    public function question_add()
    {

      $data = I('post.');
      $id = $data['id'];
      $res = M('question')->where("id='$id'")->save($data);

      if ($res) {
        Reajax(0,"修改成功");
      }else{
        Reajax(1,"修改失败");
      }

    }
    //问题列表 删除 封号还存在
    public function questiondel()
    {
      $id = I('post.id');
      // $res = M('question')->where("id='$id'")->delete();
      $sql = "update question set admin_status='1' where id='$id'";//封号还存在
      $res=M('question')->execute($sql);
      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"没有删除");
      }

    }
    //问题列表 添加
    public function question_addt()
    {
      echo "添加问题";
    }

    //删除问题--展现-------------------------------------------------------
    public function question_del()
    {

      $count      = M('question')->where("admin_status='1'")->count();// 查询满足要求的总记录数
      $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $show       = $Page->show();// 分页显示输出

      // $sql="select * from question limit ".$Page->firstRow.','.$Page->listRows;
      // // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
      // $data=M('question')->query($sql);

      $this->assign('page',$show);// 赋值分页输出

      $sql = "select t1.*,t2.name,t3.nickname from question t1 inner join category t2 on t1.cid=t2.id inner join user t3 on t1.uid=t3.id where t1.admin_status='1' limit ".$Page->firstRow.','.$Page->listRows;// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

      $res = M()->query($sql);
      // var_dump($data);
      $this->assign('data',$res);
      $this->display();
    }
    //删除问题 批量  真的删除了
    public function del_delall()
    {
      $uid = I('post.uid');
      $uid=rtrim($uid,',');
      // $id = explode(',', $uid);
      // var_dump($id);
      $sql = "delete from question where id in (".$uid.")";//真的删除了

      $res = M('question')->execute($sql);

      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"没有删除");
      }

    }
    //删除问题 删除  真的删除了
    public function questiondel_del()
    {
      $id = I('post.id');

      $res = M('question')->where("id='$id'")->delete();//真删除
      
      $res=M('question')->execute($sql);
      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"没有删除");
      }
    }
    //删除问题 恢复
    public function question_re()
    {
      $id = I('post.id');
      // var_dump($id);
      $data['admin_status']=0;

      $res = M('question')->where("id='$id'")->save($data);

      if ($res) {
        Reajax(0,"恢复成功");
      }else{
        Reajax(1,"没有恢复");
      }

    }

}
